package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.AdminIssueService;
import com.cskaoyan.market.service.impl.AdminIssueServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/issue/*")
public class AdminIssueController extends HttpServlet {

    AdminIssueService adminIssueService = new AdminIssueServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");

        if ("list".equals(op)){
            list(req, resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");

        if("create".equals(op)){
            create(req, resp);
        }
        if("update".equals(op)){
            update(req, resp);
        }
        if ("delete".equals(op)) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String readLine = req.getReader().readLine();
        MarketIssue marketIssue = objectMapper.readValue(readLine, MarketIssue.class);
        if (adminIssueService.delete(marketIssue)==1){
            Object ok = ResponseUtil.ok();
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s1);
            return;
        }
        Object fail = ResponseUtil.fail(753,"删除失败");
        String s = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s);


    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketIssue marketIssue = objectMapper.readValue(s, MarketIssue.class);

        if (adminIssueService.update(marketIssue)==1){
            Object ok = ResponseUtil.ok(marketIssue);
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s1);
            return;
        }

        Object fail = ResponseUtil.fail(752,"更新失败");
        String s1 = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s1);


    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketIssue marketIssue = objectMapper.readValue(s, MarketIssue.class);

        int i = adminIssueService.create(marketIssue);

        if (i == 1){
            Object ok= ResponseUtil.ok(marketIssue);
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s1);
            return;

        }
        if (i == 0){
            Object fail = ResponseUtil.fail(751,"创建失败");
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(s1);

        }


        resp.getWriter().flush();
        resp.getWriter().close();



    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String id = req.getParameter("id");
        String question = req.getParameter("question");
        String answer = req.getParameter("answer");
        String addTime = req.getParameter("add time");
        String localDateTime = req.getParameter("localDateTime");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        List<MarketIssue> marketIssue = adminIssueService.list(id, question, answer, addTime, localDateTime, page, limit);

        Object o = ResponseUtil.okList(marketIssue);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s);

    }
}
